import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import vueJsx from "@vitejs/plugin-vue-jsx";

export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd());
  const port = Number(env.VITE_APP_PORT_RUN) || 8888;
  return {
    base: "./",
    server: {
      host: "0.0.0.0",
      port: port,
      open: true,
      disableHostCheck: true,
      proxy: {
        // //登录
        // [env.VITE_APP_API_BASE + "/dongcheng/auth"]: {
        //   target:'http://192.168.0.149:61005',
        //   changeOrigin: true,
        //   rewrite: (path) => path.replace(env.VITE_APP_API_BASE+'/dongcheng/auth', "/dongcheng/denglu"),
        // },
        // //组织架构
        // [env.VITE_APP_API_BASE + "/dongcheng/sys"]: {
        //   target:'http://192.168.0.149:61005',
        //   changeOrigin: true,
        //   rewrite: (path) => path.replace(env.VITE_APP_API_BASE + '/dongcheng/sys', "/dongcheng/xitong"),
        // },
        //登录
        [env.VITE_APP_API_BASE + "/dongcheng/auth"]: {
          target: env.VITE_APP_API_PROXY,
          changeOrigin: true,
          rewrite: (path) => path.replace(env.VITE_APP_API_BASE,""),
        },
        //组织架构
        [env.VITE_APP_API_BASE + "/dongcheng/sys"]: {
          target: env.VITE_APP_API_PROXY,  
          changeOrigin: true,
          rewrite: (path) => path.replace(env.VITE_APP_API_BASE,""),
        },
        // //文件上传
        // [env.VITE_APP_API_BASE + "/dongcheng/mall/huaWei/doCreateObject"]: {
        //   target: 'http://192.168.0.41:45001',
        //   changeOrigin: true,
        //   rewrite: (path) => path.replace(env.VITE_APP_API_BASE, ""),
        // },
        [env.VITE_APP_API_BASE + "/dongcheng/mall"]: {
          target: env.VITE_APP_API_PROXY,
          changeOrigin: true,
          rewrite: (path) => path.replace(env.VITE_APP_API_BASE, ""),
        },
        [env.VITE_APP_API_BASE + "/dongcheng/workflow"]: {
          target: env.VITE_APP_API_PROXY,
          changeOrigin: true,
          rewrite: (path) => path.replace(env.VITE_APP_API_BASE, ""),
        },
        [env.VITE_APP_API_BASE]: {
          target: env.VITE_APP_API_PROXY,
          changeOrigin: true,
          rewrite: (path) => path.replace(env.VITE_APP_API_BASE, "")
        }
      }
    },
    plugins: [vue(), vueJsx()],
    resolve: {
      alias: {
        path: "path-browserify",
        "@": path.resolve(__dirname, "src")
      }
    },
    css: {
      preprocessorOptions: {
        less: {
          charset: false,
          modifyVars: {
            hack: `true;@import (reference)"${path.resolve(
              __dirname,
              "src/styles/variables.less"
            )}";`
          },
          javascriptEnabled: true
        }
      }
    }
  };
});
